package com.yanqu.road.server.cross3server.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.cross3server.manager.Cross3EnterNameMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Cross3Protocol.C3_ENTER_NAME_GROUPING_NAME_SYNC, desc = "分组结果下发")
public class Cross3EnterNameGroupingFromChannelCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        //全量更新缓存
        EnterNameProto.ChannelEnterNameGroupingSyncMsg syncMsg = EnterNameProto.ChannelEnterNameGroupingSyncMsg.parseFrom(yanQuMessage.getBytes());
        int activityId = syncMsg.getActivityId();

        List<ActivityGroupMatch> list = new ArrayList<>();
        for (EnterNameProto.ChannelEnterNameGroupingMsg groupingMsg : syncMsg.getMatchList()) {
            ActivityGroupMatch match = new ActivityGroupMatch();
            match.setActivityId(activityId);
            match.setGroupId(groupingMsg.getGroupId());
            match.setEnemyGroupId(groupingMsg.getEnemyGroupId());
            match.setZoneIdList(groupingMsg.getZoneListList());
            list.add(match);
        }
        Cross3EnterNameMgr.getMatchMap().put(activityId, list);
        Cross3EnterNameMgr.updateUserGroup(activityId);
        Cross3EnterNameMgr.getLogger().info("enter name activity {} cache match grouping, size {}", activityId, list.size());

        for (Map.Entry<Long, List<UserEnterName>> listEntry : Cross3EnterNameMgr.getUserNameGroupByServer(activityId).entrySet()) {
            long serverId = listEntry.getKey();
            List<UserEnterName> nameList = listEntry.getValue();
            EnterNameProto.CrossEnterNameGroupingSyncMsg.Builder sync = EnterNameProto.CrossEnterNameGroupingSyncMsg.newBuilder();
            for (UserEnterName enterName : nameList) {
                EnterNameProto.UserEnterNameMsg.Builder builder = EnterNameProto.UserEnterNameMsg.newBuilder();
                builder.setZoneId(enterName.getZoneId());
                builder.setUserId(enterName.getUserId());
                builder.setGroupId(enterName.getGroupId());
                builder.setActivityId(activityId);
                sync.addUser(builder);
            }
            sync.addAllMatch(syncMsg.getMatchList());
            sync.setActivityId(activityId);
            //通知区服，更新分组信息
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_ENTER_NAME_MATCH_GROUPING_NAME_SYNC_FROM_CROSS, sync));

            Cross3EnterNameMgr.getLogger().info("enter name activity {} sync grouping to server {} , size {}", activityId, serverId, list.size());
        }
    }
}
